﻿/* A LiveObject (Application, Service, Plugin) */
CREATE TABLE DBLiveObjects.DBLiveObject(				
	ID								uniqueidentifier PRIMARY KEY,		/* Unique GUID for this DBLiveObject */	
	DBConfigurationID				uniqueidentifier NOT NULL,			/* Related configuration */ 
	DBParentID						uniqueidentifier,					/* Parent (only applications have no parent, a service has a parent application, a plugin has a parent service */
    DBExecutionContextID			uniqueidentifier NOT NULL,			/* The context associated with this object */
	Type 							char NOT NULL,						/* 'A'pplication or 'S'ervice or 'P'lugin */
	InterfaceFullTypeName			varchar(255) NOT NULL,				/* ex: "Org.OpenTrader.Framework.Forge.IFeedService" */
	Label							varchar(255) NOT NULL,				/* Some label */
	ServicePluginInterfaceTypeName	varchar(255),						/* if 'S', the interface of the plugin */
	ListenURL						varchar(255),						/* ex: null(don't bind) or 0.0.0.0:27500 (any) or 127.0.0.1:27501 (accept only localhost) etc */
	Name							varchar(255),						/* May have a Name */


	Location						char,								/* 'L' Local or 'R' remote */

	/* If 'L'ocal */ 													/* we have a configuration, run inside a container */
	DBContainerID					uniqueidentifier,				    /* Container where the LiveObject will be isolated into */
    ClassFullTypeName				varchar(255),						/* ex: "Org.OpenTrader.Framework.Forge.FeedService" (implements IFeedService) */
	ConfigurationSchemaClassName	varchar(255),						/* a local object has a configuration */

	/* If 'R'emote */ 													/* we will obtain a proxy stub of InterfaceFullTypeName interface type */
	URL								varchar(255),                       /* ex: "remoting://192.168.168.10/MyFeedService" */
	
    FOREIGN KEY (DBParentID) REFERENCES DBLiveObjects.DBLiveObject(ID), 
    FOREIGN KEY (DBContainerID) REFERENCES DBLiveObjects.DBContainer(ID) ON DELETE SET NULL,

	CHECK(Location IN ('L','R')),										/* Local, Remote */
	CHECK(Type IN ('A','S','P')),										/* Application, Service, Plugin */
	
	_InsertionStamp							bigint,
	_UpdateStamp							bigint,
);

/*
CREATE TRIGGER Trigger_DBLiveObject_OnDelete 
ON DBLiveObjects.DBLiveObject
FOR DELETE 
AS
DECLARE @deleted_liveobject_id uniqueidentifier
SELECT @deleted_liveobject_id = (SELECT ID FROM Deleted)
DELETE FROM DBLiveObjects.DBLiveObject where DBParentID=@deleted_liveobject_id
*/